import { InnerMineEntity, SpawnEntity } from "GameLoop/WorldState/EntityManager/Entitys";
import { State } from "framework/FSM/AState";
import { ActionArgHelper } from "../../../ActionManager/ActionArgHelper";
import { PositionHelper } from "utils/position/PositionHelper";

/**精确运输任务
 * 
 */ 
export class AccurateTransportTask<T extends IStateArg<Creep>>  extends State<T>
{
    /**
     *  
     * @param arg 
     */
    public  getActions(arg:T):Action[]
    {
       
        const creep = arg.entity;
        // global.Log.Info(`TransportTask ${creep.name}`)
        if(!creep.memory.taskid)
        {

            return []
        }
        const task =arg.task as ITask<ICreepTaskData<"transportAccurate">>;
        if(!task)
        {
            return []
        }
        /*如果爬存在不需要的资源则先存起来*/
        const transortkeys = new Set<ResourceConstant>(task.data.arg.List.map(e=>e.resourceType));
        for(let k in creep.store)
        {
            if(!transortkeys.has(k as ResourceConstant))
            {
                // 清理action
                const amoubt = creep.store.getUsedCapacity(k as ResourceConstant);
                const core = arg.entity.ownerRoom().getEntity("CoreEntity");
                const store = core.getStores({resourceType:k as ResourceConstant,amount:amoubt});
                if(store.length>0)
                {
                     
                    return [ActionArgHelper.actionTransfer(arg.entity,store[0],[{resourceType:k as ResourceConstant,amount:amoubt}])]
                     
                }
                else
                {
                    arg.entity.log("error","房间放不下资源");
                }
                
            }
        }
        const action = ActionArgHelper.actionAccurateTransport(creep,task.data.arg);
        action.onError=(arg:Action)=>{
            creep.log(`warn`,`${task.id} 运输失败`,`参数异常 准备删除`);
            Memory.Debug[`精确运输任务异常${task.id}`] = JSON.stringify(task.data.arg);
            creep.room.deleteTaskById(task.id,"creep");
        }
        return [action]
     
    }
 
}
